package com.ruoyi.web.controller.cs;

import java.util.List;

import com.ruoyi.common.annotation.CheckSwitch;
import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResponsePageInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cs.domain.TTask;
import com.ruoyi.cs.service.ITTaskService;

/**
 * 任务Controller
 *
 * @author ruoyi
 * @date 2023-03-02
 */
@Api(tags = {"任务管理"})
@RestController
@RequestMapping("/cs/task")
public class TTaskController extends BasicsController
{
    private Logger log = LoggerFactory.getLogger(TVipGradeController.class);
    @Autowired
    private ITTaskService tTaskService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("查询任务列表")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @PostMapping("list")
    public ResponsePageInfo<TTask> list(@RequestBody TTask tTask)
    {
        startPage(tTask.getPageNum(),tTask.getPageSize());
        List<TTask> list = tTaskService.selectTTaskList(tTask);
        return toResponsePageInfo(list);
    }

    @ApiOperation("新增任务")
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "新增任务", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public Response add(@RequestBody TTask tTask)
    {
        return tTaskService.insertTTask(tTask);
    }


    @ApiOperation("修改任务")
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "修改任务", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public Response edit(@RequestBody TTask tTask)
    {
        return tTaskService.updateTTask(tTask);
    }

    @ApiOperation("删除任务")
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @GetMapping("/remove")
    @ApiImplicitParam(name = "id",value = "id" ,dataType = "long",required = true)
    public Response remove(Long id)
    {
        return toResponse(tTaskService.deleteTTaskById(id));
    }

    @ApiOperation("统计任务完成人数")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/countTaskCompleteNum")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime",value = "开始时间" ,dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "endTime",value = "结束时间" , dataType = "String", paramType = "query", required = false),
    })
    public Response countTaskCompleteNum(String startTime,String endTime)
    {
        return tTaskService.countTaskCompleteNum(startTime, endTime);
    }

    /**--------------------------------------前端------------------------------------------*/
    @ApiOperation("任务列表-web")
    @PostMapping("/common/taskList")
    public Response<List<TTask>> taskList(@Validated  @RequestBody TTask tTask)
    {
        String userId = null;
        LoginUser loginUser = tokenService.getLoginUserWeb(ServletUtils.getRequest());
        if(loginUser != null){
            userId = loginUser.getUser().getLoginUserId();
        }
        List<TTask> list = tTaskService.taskList(tTask,userId);
        return Response.success(list);
    }

    @CheckSwitch
    @ApiOperation("任务奖励领取-web")
    @GetMapping(value = "/taskReceive")
    @Log(title = "任务奖励领取", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "taskId",value = "任务ID" ,dataType = "long",required = true)
    public Response taskReceive(long taskId)
    {
        String userId = tokenService.getLoginUserId();
        try {
            return tTaskService.taskReceive(taskId,userId);
        } catch (Exception e) {
            log.error("任务奖励领取异常:{}",e.getMessage());
            return Response.fail("领取失败,系统异常!");
        }
    }

}
